package com.speech.utils;

import android.content.Context;

import com.speech.constant.PrefsConstant;
import com.speech.db.SharedPreferenceHelper;
import com.speech.net.result.Property_Result;

/**
 * 这个类用来保存当前用户的财产数据
 * 应用在第一次启动的时候会在HomeFragment中进行初始化
 */
public class PropertyAndUserInfoHolder {

    private static String strNickName = null;

    private static Property_Result mProperty = new Property_Result();

    /**
     * 不能直接替换对象，因为全部属性不是通过同一个接口获取到的
     *
     * @param property
     */
    public static void update(Property_Result property) {
        if (property.dTotalProperty != null && property.dTotalProperty > 0) {
            mProperty.dTotalProperty = property.dTotalProperty;
        }
        if (property.dReadingProperty != null && property.dReadingProperty > 0) {
            mProperty.dReadingProperty = property.dReadingProperty;
        }
        if (property.dCheckInProperty != null && property.dCheckInProperty > 0) {
            mProperty.dCheckInProperty = property.dCheckInProperty;
        }
        if (property.dSpreadProperty != null && property.dSpreadProperty > 0) {
            mProperty.dSpreadProperty = property.dSpreadProperty;
        }
        if (property.dMissionProperty != null && property.dMissionProperty > 0) {
            mProperty.dMissionProperty = property.dMissionProperty;
        }
        if (property.iCheckInDays != null && property.iCheckInDays > 0) {
            mProperty.iCheckInDays = property.iCheckInDays;
        }
        if (property.lServerTime != null && property.lServerTime > 0) {
            mProperty.lServerTime = property.lServerTime;
        }
        if (property.iTotalSentenceReadCnt != null && property.iTotalSentenceReadCnt > 0) {
            mProperty.iTotalSentenceReadCnt = property.iTotalSentenceReadCnt;
        }
        if (property.dCurrentAdd != null && property.dCurrentAdd > 0) {
            mProperty.dCurrentAdd = property.dCurrentAdd;
        }
        if (property.dCurrentReward != null && property.dCurrentReward > 0) {
            mProperty.dCurrentReward = property.dCurrentReward;
        }
        if (property.dTotalMinalLeft != null && property.dTotalMinalLeft > 0) {
            mProperty.dTotalMinalLeft = property.dTotalMinalLeft;
        }
        if (property.dRewardProperty != null && property.dRewardProperty > 0) {
            mProperty.dRewardProperty = property.dRewardProperty;
        }
        if (property.dSpreadPropertyLv1 != null && property.dSpreadPropertyLv1 > 0) {
            mProperty.dSpreadPropertyLv1 = property.dSpreadPropertyLv1;
        }
        if (property.dSpreadPropertyLv2 != null && property.dSpreadPropertyLv2 > 0) {
            mProperty.dSpreadPropertyLv2 = property.dSpreadPropertyLv2;
        }
        if (property.dTotalToday != null && property.dTotalToday > 0) {
            mProperty.dTotalToday = property.dTotalToday;
        }
        if (property.iTotalSentenceReadCnt != null && property.iTotalSentenceReadCnt > 0) {
            mProperty.iTotalSentenceReadCnt = property.iTotalSentenceReadCnt;
        }
        if (property.dRegisterProperty != null && property.dRegisterProperty > 0) {
            mProperty.dRegisterProperty = property.dRegisterProperty;
        }

    }

    /**
     * 用户读的句子总数
     *
     * @return
     */
    public static int getTotalReadSentence() {
        if (mProperty == null || mProperty.iTotalSentenceReadCnt == null) {
            return 0;
        }
        return mProperty.iTotalSentenceReadCnt;
    }

    public static double getUserTotalProperty() {
        if (mProperty == null || mProperty.dTotalProperty == null) {
            return 0.0d;
        }
        return mProperty.dTotalProperty;
    }

    public static Property_Result getPropertyObject() {
        return mProperty;
    }

    public static String getStrNickName(Context context) {
        if (strNickName == null) {
            SharedPreferenceHelper spf_helper = new SharedPreferenceHelper(context, PrefsConstant.FILE_USER_INFO);
            String strNickName = (String) spf_helper.getData(PrefsConstant.KEY_USER_INFO_STRING_NICKNAME, SharedPreferenceHelper.STR_TYPE_DEFAULT);
            if (strNickName.equals(SharedPreferenceHelper.STR_TYPE_DEFAULT)) {
                return "XXX";
            }
            PropertyAndUserInfoHolder.strNickName = strNickName;
        }
        return strNickName;
    }

    public static void setStrNickName(String strNickName) {
        PropertyAndUserInfoHolder.strNickName = strNickName;
    }
}
